package com.cdn.allmobilestore.ui;


import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.activity.AppCommentsActivity;
import com.cdn.allmobilestore.activity.DeveloperActivity;
import com.cdn.allmobilestore.model.AppInfo;
import com.cdn.allmobilestore.ulti.Constant;
import com.cdn.allmobilestore.ulti.InstallChecker;

public class AppDetailBottomBar extends LinearLayout
{
  protected AppInfo mAppInfo;
  protected ImageView mFirstButton;
  protected ImageView mLastButton;
  protected Context mContext;
  protected ActionBottomButton mMiddleButton;

  private View.OnClickListener mShareButtonClickListener = new View.OnClickListener()
  {
    public void onClick(View view)
    {
    	Intent intent=new Intent(android.content.Intent.ACTION_SEND);
		intent.setType("text/plain");		
		// Add data to the intent, the receiving app will decide what to do with it.
		intent.putExtra(Intent.EXTRA_SUBJECT,mAppInfo.getName());
		intent.putExtra(Intent.EXTRA_TEXT,"http://apps.allmobile.vn/detail/"+ mAppInfo.getId() +"/" );
		mContext.startActivity(Intent.createChooser(intent,"Chia sẻ"));
    }
  };
  
  public AppDetailBottomBar(Context context)
  {
    super(context);
    initViews();
  }

  public AppDetailBottomBar(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    mContext = context;
    initViews();
  }

  
  private void initViews()
  {
    View view = inflate(getContext(), R.layout.app_detail_bottom_bar, null);
    view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    addView(view);
    this.mFirstButton = ((ImageView)findViewById(R.id.first_button));
    this.mFirstButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View view)
      {
    	  Intent itent = new Intent(getContext(), AppCommentsActivity.class);
      	 itent.putExtra(Constant.INTENT_EXTRA_APP_NAME,"Tải ứng dụng:"+ mAppInfo.getName());
      	 itent.putExtra(Constant.INTENT_EXTRA_APP_ID, mAppInfo.getId());
      	 getContext().startActivity(itent);
        //InstallChecker.showPermissionDialog(mContext,AppDetailBottomBar.this.mAppInfo,false);
      }
    });
    
    this.mFirstButton.setEnabled(false);
    
    this.mLastButton = ((ImageView)findViewById(R.id.last_button));

    this.mLastButton.setOnClickListener(mShareButtonClickListener);
    
    this.mMiddleButton = ((ActionBottomButton)findViewById(R.id.middle_button));
    
  }

  public void updateActionButton(AppInfo appInfo)
  {
	  this.mFirstButton.setEnabled(true);
    this.mMiddleButton.rebind(appInfo);
  }

  public void updateViewContent(AppInfo appInfo)
  {
    if (appInfo != null)
      this.mAppInfo = appInfo;
    updateActionButton(appInfo);
  }
}
